package com.fengdi.service.impl;

import com.fengdi.mapper.ArticleMapper;
import com.fengdi.pojo.Article;
import com.fengdi.pojo.PageBean;
import com.fengdi.pojo.Result;
import com.fengdi.service.serviceInterface.ArticleService;
import com.fengdi.utils.UserThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private PageBean<Article> pageBean;

    /**
     * @param article : 新增文章的信息
     * @return 新增结果
     * @author FengDi
     * @Description 新增文章
     */
    @Override
    public Result<?> addArticle(Article article) {
        // 补充所需信息
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateTime(LocalDateTime.now());
        article.setCreateUser(UserThreadLocalUtil.getUserID());
        this.articleMapper.insertArticle(article);
        return Result.success();
    }

    /**
     * @param pageNum    当前页码
     * @param pageSize   页面大小
     * @param categoryId 所属分类
     * @param state      发布状态
     * @return 分页数据
     * @author FengDi
     * @Description 获取文章列表
     */
    @Override
    public Result<PageBean<Article>> getArticleList(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        // 创建pageBean

        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        // 调用mapper
        List<Article> list = this.articleMapper.getList(UserThreadLocalUtil.getUserID(), categoryId, state);
        Page<Article> p = (Page<Article>) list;
        // 填充数据
        this.pageBean.setTotal(p.getTotal());
        this.pageBean.setList(p.getResult());

        return Result.success(this.pageBean);
    }

    /**
     * @param id 文章Id
     * @return 文章详情数据
     * @author FengDi
     * @Description 获取文章详情
     */
    @Override
    public Result<Article> getArticleInfo(Integer id) {
        if (id != null) {
            return Result.success(this.articleMapper.getArticleById(id));
        }else {
            return Result.error("id不能为空");
        }
    }

    /**
     * @param article 新的文章详情数据
     * @return
     * @author FengDi
     * @Description 更新文章详情
     */
    @Override
    public Result<?> updateArticle(Article article) {
        if(article != null) {
            article.setUpdateTime(LocalDateTime.now());
            this.articleMapper.updateArticleById(article);
            return Result.success();
        }else {
            return Result.error("参数不合法");
        }
    }

    /**
     * @param id 删除文章的id
     * @return 删除结果
     * @author FengDi
     * @Description 删除文章
     */
    @Override
    public Result<?> deleteArticle(Integer id) {
        if(id != null) {
            this.articleMapper.deleteArticleById(id);
            return Result.success();
        }else {
            return Result.error("ID不能为空");
        }
    }
}
